package cn.ffcs.common.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Criterion;

import cn.ffcs.base.dao.BaseDAO;
import cn.ffcs.base.model.BaseEntity;
import cn.ffcs.base.web.PageControl;
import cn.ffcs.common.model.Attach;
import cn.ffcs.util.ext.Expression;

/**
 * 
 * 附件DAO实现<br/> <table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author liuzhh
 * @version $Id$
 */

public class AttachDAO<A extends Attach> extends BaseDAO<A> implements
        IAttachDAO<A>
{

    public String getEntityName()
    {
        return new Attach().getClass().getName();
    }
    
    
    /**
     * 删除所有附件
     * @param b
     * @return
     */
    public void deleteAttachByEntity(BaseEntity entity){
        getSession().createQuery("delete from Attach a where a.refEntity='"+entity.getClass().getName()+"' AND a.refEntityID="+entity.getId()).executeUpdate();
    }
    
    /**
     * 下载附件表的附件，保存文件到本地-pany_091202
     * {@inheritDoc}
     */
    public PageControl findAllByPage(int currPage, int pageSize){
        ResultSet rs=this.executeQueryBySQL("select t.id,t.full_name,t.name from t_comm_attach t where t.ref_entity='cn.ffcs.product.model.ProductReference' and is_ftp is null and to_char(end,'yyyy-mm-dd')='2009-12-01'");
        PageControl pageControl=new PageControl();
        
        try
        {
            List list =new  ArrayList();
            while(rs.next()){
                list.add(rs.getString("id"));
            }
            pageControl.setResults(list);
        }
        catch (SQLException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return pageControl;
    }

    /**
     * 执行sql查询
     * @param sql
     * @return ResultSet
     */
    public ResultSet executeQueryBySQL(String sql)
    {
        java.sql.Connection conn = null;
        java.sql.Statement stmt = null;
        Session session = null;
        ResultSet rs=null;
        try
        {
            session = super.getSession();
            conn = session.connection();
            stmt = conn.createStatement();
            rs= stmt.executeQuery(sql);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
        finally
        {
            if (session != null)
            {
                releaseSession(session);
            }
        }
        return rs;
    }


	@SuppressWarnings("unchecked")
	public List<Attach> findByEntyty(BaseEntity entity) {
		String hql=" from Attach a where a.refEntity='"+entity.getClass().getName()+"' AND a.end>sysdate AND a.refEntityID="+entity.getId();
		List<Attach> list=new ArrayList<Attach>();
		list=getHibernateTemplate().find(hql);
		return list;
	}

	public List<Attach> findAttachList(String type,String appId) {
		String hql="select a from Attach a where a.refEntity=? AND a.refEntityID=? AND a.end>sysdate";
		List<Attach> list=getHibernateTemplate().find(hql,new Object[]{type,appId});
		return list;
	}
	
	public void batchDelAttach(String type,String appId){
		String hql="delete from Attach a where a.refEntity=? AND a.refEntityID=?";
		getHibernateTemplate().bulkUpdate(hql, new Object[]{type,appId});
	}
}
